package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.CheckStatusEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.Company;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.mapper.CompanyMapper;
import com.qinggeng.project.system.service.ICompanyService;
import com.qinggeng.project.system.service.IJobUserService;
import com.qinggeng.project.system.service.IPositionClassifyService;
import com.qinggeng.project.system.service.IPositionNameService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompanyServiceImpl implements ICompanyService {
    @Resource
    private CompanyMapper companyMapper;
    @Autowired
    private IJobUserService jobUserService;
    @Autowired
    private IPositionClassifyService positionClassifyService;

    @Override
    public boolean insertCompany(Company company) {
        company.setClassifyName(positionClassifyService.checkPositionClassify(company.getClassifyCode()).getName());
        company.setStatus(CheckStatusEnum.审核中.getValue());
        company.setCreateTime(DateUtils.getNowDate());
        return companyMapper.insertCompany(company)>0;
    }

    @Override
    public boolean deleteCompanyById(Long id) {
        JobUser jobUser=jobUserService.getJobUser();
        this.getCompanyByIdAndJobUserId(jobUser,id);
        return companyMapper.deleteCompanyById(id)>0;
    }

    @Override
    public int deleteCompanyByIds(Long[] ids) {
        return companyMapper.deleteCompanyByIds(ids);
    }

    @Override
    public boolean updateCompany(Company company) {
        company.setClassifyName(positionClassifyService.checkPositionClassify(company.getClassifyCode()).getName());
        company.setStatus(CheckStatusEnum.审核中.getValue());
        company.setUpdateTime(DateUtils.getNowDate());
        return companyMapper.updateCompany(company)>0;
    }

    @Override
    public boolean checkCompany(Company company) {
        company.setUpdateTime(DateUtils.getNowDate());
        return companyMapper.updateCompany(company)>0;
    }

    @Override
    public List<Company> listCompany(Company where) {
        return companyMapper.listCompany(where);
    }


    @Override
    public List<Company> listCompanyByJobUserId() {
        JobUser jobUser=jobUserService.getJobUser();
        Company where=new Company();
        where.setJobUserId(jobUser.getJobUserId());
        List<Company> companyList=companyMapper.listCompany(where);
        Company defaultCompany=this.getDefaultCompany(jobUser);
        for (Company company:companyList
        ) {
            if(null==defaultCompany||defaultCompany.getId().compareTo(company.getId())!=0){
                continue;
            }
            company.setIsDefault(true);
        }
        return companyList;
    }







    @Override
    public Company getCompanyById(Long id) {
        Company company=companyMapper.getCompanyById(id);
        if(null==company){
            throw new CustomException("公司不存在或已被删除");
        }
        return company;
    }

    @Override
    public Company getCompanyByIdAndJobUserId(JobUser jobUser,Long id) {
        Company company=companyMapper.getCompanyByIdAndJobUserId(id,jobUser.getJobUserId());
        if(null==company){
            throw new CustomException("公司不存在或没操作权限！", ShowTypeEnum.用户.getValue());
        }
        if(jobUser.getCompanyId().compareTo(company.getId())==0){
            company.setIsDefault(true);
        }
        return company;
    }

    @Override
    public boolean checkCompanyIsExists(Long jobUserId) {
        return this.getCompanyNumByJobUserId(jobUserId,null)>0;
    }

    @Override
    public Integer getCompanyNumByJobUserId(Long jobUserId,String status) {
        return companyMapper.getCompanyNumByJobUserId(jobUserId,status);
    }

    @Override
    public Company getDefaultCompany(JobUser jobUser) {
        if (null == jobUser) {
            return null;
        }
        Company company =companyMapper.getCompanyByIdAndJobUserId(jobUser.getCompanyId(),jobUser.getJobUserId());
        if (company!=null&&company.getStatus().equals(CheckStatusEnum.已通过.getValue())) {
            return company;
        }
        company=companyMapper.getDefaultCompanyByJobUserId(jobUser.getJobUserId());
        if(null==company||null==company.getId()){
            return null;
        }
        this.setDefaultCompany(jobUser,company.getId());
        return company;
    }

    @Override
    public boolean setDefaultCompany(JobUser jobUser,Long companyId){
        if(null==jobUser){
            return false;
        }
        if(jobUser.getCompanyId()!=null&&jobUser.getCompanyId().compareTo(companyId)==0){
            return true;
        }
        jobUser.setCompanyId(companyId);
        return jobUserService.updateJobUser(jobUser);
    }
}
